﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DataTransfer;
namespace Gui
{
    public partial class frmDanhSachBenhNhan : DevComponents.DotNetBar.Office2007Form
    {
        frmMain frm_main;
        public frmDanhSachBenhNhan(frmMain frm)
        {
            frm_main = frm;
            InitializeComponent();
           
        }
        private BenhNhanBus dsbnBus;
        private void frmDanhSachKhamBenh_Load(object sender, EventArgs e)
        {
            load();
        }


        public void load()
        {
            dsbnBus=new BenhNhanBus();
            
            dtgDanhSachBenhNhan.DataSource = dsbnBus.GetAll();
            dtgDanhSachBenhNhan.Columns["IDPK"].Visible = false;
        }

        private void dtpNgayKham_ValueChanged(object sender, EventArgs e)
        {
            dsbnBus = new BenhNhanBus();
            DateTime key = dtpNgayKham.Value.Date;
            dtgDanhSachBenhNhan.DataSource = dsbnBus.GetByDay(key);
            dtgDanhSachBenhNhan.Columns["IDPK"].Visible = false;

        }

      

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLapHoaDon_Click(object sender, EventArgs e)
        {
            BenhNhan bn = new BenhNhan();
            getItem(ref bn);
            frm_main.LapHoaDon(bn);
            //Form frm = new frmLapHoaDon( bn,frm_main);


        }

        private bool getItem(ref BenhNhan info)
        {
            bool flag = false;
            int index = dtgDanhSachBenhNhan.CurrentRow.Index;
            if (index >= 0)
            {

                info.ID = Convert.ToInt32(dtgDanhSachBenhNhan["ID", index].Value.ToString());
                info.HoTen = dtgDanhSachBenhNhan["HoTen", index].Value.ToString();
                //info.GioiTinh = Convert.ToInt32(dtgDanhSachBenhNhan["GioiTinh", index].Value.ToString());
                //info.NamSinh = Convert.ToDateTime(dtgDanhSachBenhNhan["NamSinh", index].Value.ToString());
                //info.DiaChi = dtgDanhSachBenhNhan["DiaChi", index].Value.ToString();
                info.NgayKham = Convert.ToDateTime(dtgDanhSachBenhNhan["NgayKham", index].Value.ToString());
                info.IDPhieuKham = Convert.ToInt32(dtgDanhSachBenhNhan["IDPK", index].Value.ToString());
                flag = true;
            }
            if (flag == false)
            {
                MessageBox.Show("Chua chon Item");
            }
            return flag;
        }


        private void btnXoaBenhNhan_Click(object sender, EventArgs e)
        {
            BenhNhan info = new BenhNhan();
            if (getItem(ref info) == true)
            {
                try
                {
                    if (dsbnBus.Delele(info.ID))
                    {
                        DeletePhieuKham(Convert.ToInt32(info.IDPhieuKham));
                        DelelePhieuKhamChiTiet(Convert.ToInt32(info.IDPhieuKham));
                        MessageBox.Show("Delete Success");
                        load();
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Delete Error:" + ex.Message);
                    //throw;
                }
            }
        }
        private bool DeletePhieuKham(int key)
        {
            PhieuKhamBus pkbus = new PhieuKhamBus();
            return pkbus.Delele(key);
        }
        private bool DelelePhieuKhamChiTiet(int key)
        {
            PhieuKhamChiTietBus pkctBus = new PhieuKhamChiTietBus();
            return pkctBus.DeleleById(key);
        }

       

        
       
    }
}
